package org.yoao.crm.business.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.yoao.crm.bean.User;
import org.yoao.crm.business.service.IUserService;
import org.yoao.crm.dao.CustomerDAO;
import org.yoao.crm.dao.RoleDAO;
import org.yoao.crm.dao.UserDAO;
import org.yoao.crm.struts.form.UserForm;
import org.yoao.crm.tools.TimeTool;

/**
 * @author yxwonline.com
 * @description
 * @date 2010-7-27 下午09:44:33
 */
public class UserServiceImpl implements IUserService {
	private UserDAO userDAO = new UserDAO();

	@Override
	public List view(int startRow, int endRow, Map map) throws Exception {
		return userDAO.find(startRow, endRow, map);
	}

	@Override
	public int getDataCount(Map map) throws Exception {
		return userDAO.getDataCount(map);
	}

	@Override
	public UserForm add(UserForm form) throws Exception {
		User user = new User();
		BeanUtils.copyProperties(user, form);
		User tmp = userDAO.findUserByName(user.getUserName());
		if (tmp != null) {
			form.setInfo("用户名已存在,重复!");
		} else {
			user.setCreateTime(TimeTool.getSystemTime());
			user.setAccessSno(user.getRole() + ";"
					+ new RoleDAO().findAllParentID(user.getRole()));
			int result = userDAO.insert(user);
			if (result > -1) {
				form.setId(result);
				form.setInfo("数据保存成功.");
			} else
				form.setInfo("数据保存失败.");
		}
		return form;
	}

	@Override
	public UserForm modify(UserForm form) throws Exception {
		User user = new User();
		BeanUtils.copyProperties(user, form);
		// 获取新的角色链
		user.setAccessSno(user.getRole() + ";"
				+ new RoleDAO().findAllParentID(user.getRole()));
		if (userDAO.modify(user) > -1) {
			// 修改该用户的客户权限
			if (new CustomerDAO().modifyRole(user.getAccessSno(), user
					.getUserName()) > -1)
				form.setInfo("数据保存成功.");
			else
				form.setInfo("用户修改成功,但用户该用户的客户访问权限更新失败!");
		} else
			form.setInfo("数据保存失败.");
		return form;
	}

	@Override
	public void delete(String id) throws Exception {
		String[] idArr = id.split(",");
		if (idArr != null && idArr.length > 0) {
			for (int i = 0; i < idArr.length; i++) {
				userDAO.delete(idArr[i]);
			}
		}
	}

	@Override
	public UserForm getUser(String id) throws Exception {
		UserForm form = new UserForm();
		User user = userDAO.findById(id);
		if (user != null)
			BeanUtils.copyProperties(form, user);
		return form;
	}

	@Override
	public void audit(String id, String type) throws Exception {
		String[] idArr = id.split(",");
		if (idArr != null && idArr.length > 0) {
			int value = 0;
			if ("1".equals(type))
				value = 1;
			for (int i = 0; i < idArr.length; i++) {
				userDAO.audit(idArr[i], value);
			}
		}
	}

	@Override
	public String modifyPassWord(String nickName, String userName,
			String opassWord, String passWord) throws Exception {
		User user = userDAO.findUserByName(userName);
		if (!user.getPassWord().equals(opassWord)) {
			return "旧密码错误,密码修改失败.";
		}
		if (userDAO.modifyPassWord(nickName, userName, passWord) > 0)
			return "密码修改成功.";
		else
			return "密码修改失败.";
	}

	@Override
	public String getRoleByUser(String userSno) throws Exception {
		Map map = userDAO.findRoleByUser(userSno);
		if (map == null || map.isEmpty())
			return "";
		return map.get("ROLEID") + "|" + map.get("ROLENAME");
	}
}